"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const defines_1 = require("../lib/defines");
const { obtainMicrophoneAccess, releaseMicrophoneAccess } = defines_1.RequestMethod;
function microphone(app) {
    const { socket, rooms } = app.locals;
    const { userId, username } = socket.handshake.query;
    socket.on(obtainMicrophoneAccess, function obtainMicrophone(room, cb) {
        const member = { userId, username };
        const __room = rooms[room.classId];
        if (!__room.hasSpeaker()) {
            __room.setSpeaker(member);
            socket
                .to(room.classId)
                .emit(obtainMicrophoneAccess, { room, member }, () => {
                cb && cb({ status: 'ok' });
            });
        }
        else {
            cb && cb({ status: 'reject' });
        }
    });
    socket.on(releaseMicrophoneAccess, function releaseMicrophone(room, cb) {
        const member = { userId, username };
        const __room = rooms[room.classId];
        if (__room.hasSpeaker()) {
            const speaker = __room.getSpeaker();
            if ((speaker === null || speaker === void 0 ? void 0 : speaker.userId) === userId) {
                __room.removeSpeaker();
                socket
                    .to(room.classId)
                    .emit(releaseMicrophoneAccess, { room, member }, () => {
                    cb && cb({ status: 'ok' });
                });
                return;
            }
        }
        cb && cb({ status: 'reject' });
    });
}
exports.default = microphone;
